package com.black.platform.organization.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.black.platform.organization.po.Organization;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface OrganizationMapper extends BaseMapper<Organization> {

    /**
     * 获取所有权限归属机构信息
     * @param comCode          归属机构代码
     * @param isIncludeInvalid 是否包含无效的
     * @return 所有权限归属机构信息
     */
    @Select("<script>" +
            "WITH RECURSIVE temp AS ( " +
            "SELECT * FROM organization o "+
            "<where>" +
            "<if test='comCode!=null and comCode!=\"\"'>" +
            "o.com_code=#{comCode} " +
            "</if>" +
            "<if test='!isIncludeInvalid'>" +
            "AND o.valid=1 " +
            "</if>" +
            "</where>" +
            "UNION ALL "+
            "SELECT o.* FROM organization o,temp t WHERE t.com_code=o.upper_code "+
            "<if test='!isIncludeInvalid'>" +
            "AND o.valid=1 " +
            "</if>" +
            ")SELECT DISTINCT * FROM temp order by com_code"+
            "</script>")
    List<Organization> getOrganizationsByPowerComCode(@Param("comCode") String comCode, @Param("isIncludeInvalid") boolean isIncludeInvalid);

    /**
     * 获取所有权限归属机构代码
     *
     * @param comCode 归属机构代码
     * @return 所有权限归属机构代码 格式为 comCode,comCode...
     */
    @Select("<script>" +
            "WITH RECURSIVE temp AS ( " +
            "SELECT com_code FROM organization o "+
            "WHERE o.com_code=#{comCode} AND o.valid=1 " +
            "UNION ALL "+
            "SELECT o.com_code FROM organization o,temp t WHERE t.com_code=o.upper_code AND o.valid=1 " +
            ")SELECT group_concat(com_code) FROM temp"+
            "</script>")
    String getPowerComCode(@Param("comCode") String comCode);
}
